﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUUserToView
    {
        /// <summary>
        /// Get All the UserToView of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<UserToView> GetAllUserToView(bool searchInDeleted)
        {
            List<UserToView> UserToViews = new List<UserToView>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from UserToView in db.UserToView
                              where UserToView.utv_isdeleted == searchInDeleted
                              select UserToView;
                foreach (UserToView tmp in results)
                    UserToViews.Add(tmp);
            }

            return UserToViews;
        }

        /// <summary>
        /// Get the UserToView by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UserToView GetUserToViewByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var utv = from UserToView in db.UserToView
                              where UserToView.utv_id == id
                              select UserToView;
                    foreach (UserToView tmp in utv)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// utv a UserToView into the database.
        /// </summary>
        /// <param name="utv"></param>
        /// <returns></returns>
        public static UserToView UserToViewrea(UserToView utv)
        {
            if (utv != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    UserToView nutv = db.UserToView.Add(utv);
                    db.SaveChanges();
                    return nutv;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific UserToView of the database.
        /// </summary>
        /// <param name="utv"></param>
        /// <returns></returns>
        public static bool RemoveUserToView(UserToView utv)
        {
            if (utv != null)
            {
                utv.utv_isdeleted = true;
                if (UpdateUserToView(utv) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the UserToView into the database.
        /// </summary>
        /// <param name="utv"></param>
        /// <returns></returns>
        public static UserToView UpdateUserToView(UserToView utv)
        {
            if (utv != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from UserToView in db.UserToView
                                  where UserToView.utv_id == utv.utv_id
                                  select UserToView;

                    foreach (var tmp in current)
                    {
                        tmp.usr_viewer_id = utv.usr_viewer_id;
                        tmp.usr_subject_id = utv.usr_viewer_id;
                        tmp.utv_date = utv.utv_date;
                        tmp.utv_isdeleted = utv.utv_isdeleted;

                        db.SaveChanges();
                    }

                    utv = current as UserToView;
                }
            }

            return utv;
        }
    }
}
